package com.firebird.app.kotlin.stock.ui.login.presenter

import com.firebird.app.kotlin.stock.base.BasePresenter
import com.firebird.app.kotlin.stock.data.LoginBean
import com.firebird.app.kotlin.stock.ui.login.contract.LoginContract
import com.firebird.app.kotlin.stock.ui.login.model.LoginModel
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class LoginPresenter : BasePresenter<LoginContract.View?>(), LoginContract.Presenter {
    private val model: LoginContract.Model
    override fun getData(body: RequestBody?) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getData(body)!!.enqueue(object : Callback<LoginBean?> {
            override fun onResponse(call: Call<LoginBean?>, response: Response<LoginBean?>) {
                mView!!.hideLoading()
                mView!!.onSuccess(response.body(), response.headers()["token"])
            }

            override fun onFailure(call: Call<LoginBean?>, t: Throwable) {
                mView!!.hideLoading()
                mView!!.onError(t)
            }
        })
    }

    init {
        model = LoginModel()
    }
}